package cn.cmeizu.spring6.validator.two;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;

import java.util.List;

/**
 * @author cmeizu
 * @date 2023/2/19 18:56
 * spring里的校验器
 */
@Service
public class MyValidation2 {

  @Autowired
  private Validator validator;

  public boolean validatorByUserTwo(User user) {
    BindException bindException = new BindException(user, user.getName());
    validator.validate(user, bindException);
    List<ObjectError> allErrors = bindException.getAllErrors();
    System.out.println("allErrors = " + allErrors);
    return bindException.hasErrors();
  }
}
